﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using Dapper;

namespace LibMS.Models
{
    public interface IRepository<T>
    {
        IEnumerable<T> GetData(out int? total, out string sqlMessage, DynamicParameters parameters = null);

        T GetById(out string sqlMessage, DynamicParameters parameters);

        int? Create(out string sqlMessage, DynamicParameters parameters);

        int? Update(out string sqlMessage, DynamicParameters parameters);

        int? Delete(out string sqlMessage, DynamicParameters parameters);

        IEnumerable<T> QuerySp(out int? total, out string sqlMessage, string spName, DynamicParameters parameters = null);

        IEnumerable<T> QuerySpNT(out string sqlMessage, string spName, DynamicParameters parameters = null);

        T QuerySp(out string sqlMessage, string spName, DynamicParameters parameters = null);

        int? ExecuteSp(out string sqlMessage, string spName, DynamicParameters parameters = null);
    }

    public class Repository<T> : DapperSqlHelper<T>, IRepository<T> where T : class
    {
        private string _dbTableName;

        public Repository(IDbConnection dbConnection, string tableName)
            : base(dbConnection)
        {
            _dbConnection = dbConnection;
            _dbTableName = tableName;
        }

        public override IEnumerable<T> QuerySp(out int? total, out string sqlMessage, string spName, DynamicParameters parameters = null)
        {
            return base.QuerySp(out total, out sqlMessage, spName, parameters);
        }

        public override IEnumerable<T> QuerySpNT(out string sqlMessage, string spName, DynamicParameters parameters = null)
        {
            return base.QuerySpNT(out sqlMessage, spName, parameters);
        }

        public override T QuerySp(out string sqlMessage, string spName, DynamicParameters parameters = null)
        {
            return base.QuerySp(out sqlMessage, spName, parameters);
        }

        public override int? ExecuteSp(out string sqlMessage, string spName, DynamicParameters parameters = null)
        {
            return base.ExecuteSp(out sqlMessage, spName, parameters);
        }

        T IRepository<T>.GetById(out string sqlMessage, DynamicParameters parameters)
        {
           return QuerySp(out sqlMessage, _dbTableName + StringConstant.SP_BASE_GET_BYID, parameters);
        }

        public IEnumerable<T> GetData(out int? total, out string sqlMessage, DynamicParameters parameters = null)
        {
            return QuerySp(out total, out sqlMessage, _dbTableName + StringConstant.SP_BASE_GET, parameters);
        }

        public int? Create(out string sqlMessage, DynamicParameters parameters)
        {
            return ExecuteSp(out sqlMessage, _dbTableName + StringConstant.SP_BASE_CREATE, parameters);
        }

        public int? Update(out string sqlMessage, DynamicParameters parameters)
        {
            return ExecuteSp(out sqlMessage, _dbTableName + StringConstant.SP_BASE_UPDATE, parameters);
        }

        public int? Delete(out string sqlMessage, DynamicParameters parameters)
        {
            return ExecuteSp(out sqlMessage, _dbTableName + StringConstant.SP_BASE_DELETE, parameters);
        }
    }
}